package com.kws.merchant.mapper;

import com.kws.merchant.domain.vo.GoodsVo;
import com.kws.merchant.domain.vo.MerchantDetailVo;
import com.kws.system.api.domain.SysDictData;
import jdk.nashorn.internal.objects.annotations.Setter;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author : zhangbenting
 * @date : 2024/10/30 19:32
 * @Version: 1.0
 */
public interface GoodsMapper {

    @Select(
            "<script>" +
                    "SELECT " +
                    " count(a.id) as `count` " +
                    " FROM " +
                    " tb_device AS a " +
                    " LEFT JOIN tb_merchant AS b ON a.merchant_id = b.id  " +
                    " WHERE " +
                    " a.`status` = 0" +
                    " <if test='name!=null and name!=\"\"'>  AND a.name like '%${name}%'  </if>  " +
                    " <if test='merchantName!=null and merchantName!=\"\"'>  AND b.company_name = '${merchantName}'  </if>  " +
                    " <if test='type!=null and type!=\"\"'>  AND a.type = '${type}'  </if>  " +
                    " <if test='shopsId != null'>  AND b.id = #{shopsId}  </if>  " +
                    " <if test='deviceTag != null'>  AND a.device_tag = #{deviceTag}  </if>  " +
                    "</script>")
    Long goodsCount(@Param("name") String name,
                    @Param("merchantName") String merchantName,
                    @Param("type") String type,
                    @Param("shopsId") Long shopsId,
                    @Param("deviceTag") Integer deviceTag);

    @Select(
            "<script>" +
                    "SELECT " +
                    " a.id as goods_id, " +
                    " a.`name`, " +
                    " a.type, " +
                    " a.price, " +
                    " a.price_tag, " +
                    " a.img, " +
                    " a.remark, " +
                    " a.device_tag, " +
                    " b.company_name as shops_name, " +
                    " b.id as shops_id  " +
                    " FROM " +
                    " tb_device AS a " +
                    " LEFT JOIN tb_merchant AS b ON a.merchant_id = b.id  " +
                    " WHERE " +
                    " a.`status` = 0 " +
                    " <if test='name != null and name != \"\"'>  AND a.name like '%${name}%'  </if>  " +
                    " <if test='merchantName != null and merchantName != \"\"'>  AND b.company_name like '%${merchantName}%'  </if>  " +
                    " <if test='type != null and type != \"\"'>  AND a.type = '${type}'  </if>  " +
                    " <if test='shopsId != null'>  AND b.id = #{shopsId}  </if>  " +
                    " <if test='deviceTag != null'>  AND a.device_tag = #{deviceTag}  </if>  " +
                    " limit #{page},#{size}" +
                    "</script>")
    List<GoodsVo> goodsList(@Param("name") String name,
                            @Param("merchantName") String merchantName,
                            @Param("type") String type,
                            @Param("shopsId") Long shopsId,
                            @Param("deviceTag") Integer deviceTag,
                            @Param("page") Integer page,
                            @Param("size") Integer size);

    @Select("SELECT " +
            " *  " +
            "FROM " +
            " sys_dict_data sdd  " +
            "WHERE " +
            " sdd.STATUS = '1'  " +
            " AND sdd.dict_type = '${dictType}'  " +
            "ORDER BY " +
            " sdd.dict_sort ASC")
    List<SysDictData> getDictInfo(@Param("dictType") String dictType);

    @Select("SELECT " +
            " a.id AS merchant_id, " +
            " a.logo as shop_img, " +
            " a.company_name AS merchant_name  " +
            "FROM " +
            " `tb_merchant` AS a " +
            " INNER JOIN ( " +
            " SELECT " +
            "  merchant_id, " +
            "  COUNT(*) AS occurrence  " +
            " FROM " +
            "  tb_order  " +
            " WHERE " +
            "  type = 0  AND" +
            "  merchant_id IS NOT NULL  " +
            "  AND merchant_id != ''  " +
            " GROUP BY " +
            "  merchant_id  " +
            " ORDER BY " +
            "  occurrence DESC  " +
            "  LIMIT 20  " +
            " ) AS b ON a.id = b.merchant_id  " +
            " LIMIT 20")
    List<MerchantDetailVo> getVideoNumberDevice();
}
